//
// Created by konglinglong on 2020/9/24.
//

#include <stdio.h>
#include <iostream>
#include "../lib/doctest.h"
#include "../../include/osal/osSelect.h"

TEST_SUITE ("OS_SELECT_TS")
{
    TEST_CASE ("OS_SELECT_TC")
    {
        int32_t rc;
        uint32_t loop;
        uint32_t count;
        S_OsSelect selector;
        S_OsSelectInd sendInd;
        S_OsSelectInd recvInd;

        SUBCASE("OS_SELECT_SC_BASE")
        {
            rc = OS_SelectInit(&selector, NULL);
            REQUIRE(CSP_RC_OK == rc);
            rc = OS_SelectClear(&selector);
            REQUIRE(CSP_RC_OK == rc);
            rc = OS_SelectFinal(&selector);
            REQUIRE(CSP_RC_OK == rc);
        }

        SUBCASE("OS_SELECT_SC_SEND_RECV")
        {
            rc = OS_SelectInit(&selector, NULL);
            REQUIRE(CSP_RC_OK == rc);

            for (loop = 0; loop < 256; loop++)
            {
                sendInd.ind = loop;
                rc = OS_SelectSend(&selector, &sendInd);
                REQUIRE(CSP_RC_OK == rc);
                rc = OS_SelectRecv(&selector, &recvInd);
                REQUIRE(CSP_RC_OK == rc);
                REQUIRE(recvInd.ind == loop);
            }

            rc = OS_SelectFinal(&selector);
            REQUIRE(CSP_RC_OK == rc);
        }

        SUBCASE("OS_SELECT_SC_SEND_RECV_1")
        {
            rc = OS_SelectInit(&selector, NULL);
            REQUIRE(CSP_RC_OK == rc);

            count = 0;

            while (1)
            {
                sendInd.ind = count;
                rc = OS_SelectSend(&selector, &sendInd);
                if (CSP_RC_OK != rc)
                {
                    break;
                }
                count++;
            }
            for (loop = 0; loop < count; loop++)
            {
                rc = OS_SelectRecv(&selector, &recvInd);
                REQUIRE(CSP_RC_OK == rc);
                REQUIRE(recvInd.ind == loop);
            }

            rc = OS_SelectFinal(&selector);
            REQUIRE(CSP_RC_OK == rc);
        }

        SUBCASE("OS_SELECT_SC_SEND_RECV_NAME_PIPE")
        {
            rc = OS_SelectInit(&selector, "selector_test");
            REQUIRE(CSP_RC_OK == rc);

            for (loop = 0; loop < 256; loop++)
            {
                sendInd.ind = loop;
                rc = OS_SelectSend(&selector, &sendInd);
                REQUIRE(CSP_RC_OK == rc);
                rc = OS_SelectRecv(&selector, &recvInd);
                REQUIRE(CSP_RC_OK == rc);
                REQUIRE(recvInd.ind == loop);
            }

            rc = OS_SelectFinal(&selector);
            REQUIRE(CSP_RC_OK == rc);
        }

        SUBCASE("OS_SELECT_SC_SEND_RECV_1_NAME_PIPE")
        {
            rc = OS_SelectInit(&selector, "selector_test");
            REQUIRE(CSP_RC_OK == rc);

            count = 0;

            while (1)
            {
                sendInd.ind = count;
                rc = OS_SelectSend(&selector, &sendInd);
                if (CSP_RC_OK != rc)
                {
                    break;
                }
                count++;
            }
            for (loop = 0; loop < count; loop++)
            {
                rc = OS_SelectRecv(&selector, &recvInd);
                REQUIRE(CSP_RC_OK == rc);
                REQUIRE(recvInd.ind == loop);
            }

            rc = OS_SelectFinal(&selector);
            REQUIRE(CSP_RC_OK == rc);
        }
    }
}

